package com.google.code.mamule.mail;

import com.google.common.base.Objects;

import javax.persistence.Embeddable;

@Embeddable
public class Address {

  private String residence;
  private String locality;
  private String postcode;
  private String country;

  public Address() { /* JAXB */
  }

  public Address(final String residence, final String locality, final String postcode,
                 final String country) {
    super();
    this.residence = residence;
    this.locality = locality;
    this.postcode = postcode;
    this.country = country;
  }

  /**
   * @return most specific location, e.g. residence name + number of house
   */
  public String getResidence() {
    return residence;
  }

  /**
   * @return the next biggest postal area, e.g. a city
   */
  public String getLocality() {
    return locality;
  }

  /**
   * @return unique, country specific identifier of the postal area
   */
  public String getPostcode() {
    return postcode;
  }

  /**
   * @return country code of the receiver's state of residence
   */
  public String getCountry() {
    return country;
  }

  public void setResidence(final String residence) {
    this.residence = residence;
  }

  public void setLocality(final String locality) {
    this.locality = locality;
  }

  public void setPostcode(final String postcode) {
    this.postcode = postcode;
  }

  public void setCountry(final String country) {
    this.country = country;
  }

  @Override
  public String toString() {
    return Objects.toStringHelper(this)
        .add("residence", residence)
        .add("locality", locality)
        .add("postcode", postcode)
        .add("country", country)
        .toString();
  }

  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    final Address address = (Address) o;

    if (country != null ? !country.equals(address.country) : address.country != null) {
      return false;
    }
    if (locality != null ? !locality.equals(address.locality) : address.locality != null) {
      return false;
    }
    if (postcode != null ? !postcode.equals(address.postcode) : address.postcode != null) {
      return false;
    }
    if (residence != null ? !residence.equals(address.residence) : address.residence != null) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = residence != null ? residence.hashCode() : 0;
    result = 31 * result + (locality != null ? locality.hashCode() : 0);
    result = 31 * result + (postcode != null ? postcode.hashCode() : 0);
    result = 31 * result + (country != null ? country.hashCode() : 0);
    return result;
  }
}
